﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Components;

namespace WorkflowDesigner
{
    public class ToolItem : StackPanel
    {
        public event OnSelectToolItem SelectToolItem;

        #region private fields
        private readonly Image _Icon;
        private readonly TextBlock _Label;
        #endregion

        #region props
        public string ToolItemName { get; set; }
        public string Description { get; set; }
        public string IconFileName { get; set; }
        public Size IconSize { get; set; }
        public WorkflowComponentBase UserObject { get; set; }
        public string Category
        {
            get
            {
                if (this.UserObject != null && !string.IsNullOrEmpty(this.UserObject.ComponentCategory))
                    return this.UserObject.ComponentCategory;
                return "Unknown";
            }
        }
        public bool Selected { get; set; }
        #endregion

        #region ctor
        public ToolItem():this("New activity","Description","accessibilitycpl_339.ico")
        {}

        public ToolItem(WorkflowComponentBase component)
            :this(component.ComponentName, component.Description, component.ComponentIconName)
        {
            this.UserObject = component;
        }

        public ToolItem(string name, string desc, string iconName)
        {
            this.Orientation = Orientation.Horizontal;
            this.SetValue(DockPanel.DockProperty, Dock.Top);
            
            this.HorizontalAlignment = HorizontalAlignment.Stretch;
            
            this.ToolItemName = name;
            this.Description = desc;
            this.IconSize = new Size(16, 16);
            string imgUrl = string.Format("images/{0}", iconName);
            BitmapImage bmpImg = new BitmapImage(new Uri(imgUrl, UriKind.RelativeOrAbsolute));
            this._Icon = new Image();
            this._Icon.Source = bmpImg;
            this._Icon.SetValue(WidthProperty, this.IconSize.Width);
            this._Icon.SetValue(HeightProperty, this.IconSize.Height);
            if (this._Icon != null)
            {
                this.Children.Add(this._Icon);
            }

            TextBlock space=new TextBlock();
            space.Width = 10;
            space.Text = string.Empty;
            this.Children.Add(space);

            this._Label = new TextBlock();
            this._Label.Margin=new Thickness(0,2,0,4);
            this._Label.HorizontalAlignment = HorizontalAlignment.Stretch;
            this._Label.Text = this.ToolItemName;
            this.Children.Add(this._Label);

            this.Loaded += new RoutedEventHandler(ToolItem_Loaded);
        }
        #endregion

        #region load/init/draw
        void ToolItem_Loaded(object sender, RoutedEventArgs e)
        {
            this.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseLeftButtonDown);
            //this._StackPanel.MouseLeftButtonUp += new MouseButtonEventHandler(OnMouseLefButtonUp);
            //this._StackPanel.MouseMove += new MouseEventHandler(OnMosueMove);
            this.MouseEnter += new MouseEventHandler(OnMouseEnter);
            this.MouseLeave += new MouseEventHandler(OnMouseLeave);

            if(this.Parent!=null)
                this.SetValue(WidthProperty, (double)this.Parent.GetValue(WidthProperty));
        }
        #endregion

        #region mouse event handler
        void OnMouseLeave(object sender, MouseEventArgs e)
        {
            this._Label.FontWeight = FontWeights.Normal;
        }

        void OnMouseEnter(object sender, MouseEventArgs e)
        {
            this._Label.FontWeight = FontWeights.Bold;
        }

        //private void OnMosueMove(object sender, MouseEventArgs e)
        //{
        //    if(this.Selected && this.UserObject !=null)
        //    {
        //        if (this.DragToolItem != null)
        //            this.DragToolItem(this.UserObject);
        //    }
        //}

        //private void OnMouseLefButtonUp(object sender, MouseButtonEventArgs e)
        //{
        //    this.Selected = false;
        //    if (this.DragToolItem != null)
        //        this.DragToolItem(null);
        //}

        private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Selected = true;
            if (this.SelectToolItem != null)
                this.SelectToolItem(this.UserObject);
        }
        #endregion
    }
}
